﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Tradukisto.DataAccess;
using Tradukisto.Engine.Utilities;
using Tradukisto.Model;
using Tradukisto.Model.Special;

namespace Tradukisto.Controllers
{
    public class LocalManagerController : Controller
    {
        //
        // GET: /LocalManager/

        public ActionResult Index()
        {
            ViewData["ControllerName"] = new SelectList(Manager.GetControllers(""), "Name", "Name");
            ViewData["ActionName"] = new SelectList(Manager.GetActions("", ""), "Name", "Name");
            ViewData["Culture"] = new SelectList(Manager.GetCultures(""), "Name", "Name");
            return View();
        }

        [OutputCache(Duration = 0, NoStore = true)]
        public PartialViewResult FilterItems(FilterItems filterValues)
        {
            var ret = Manager.FilterLocalItems(filterValues);
            return PartialView("_FilterResults", ret);
        }

        [HttpGet]
        public PartialViewResult CreateNew()
        {
            return PartialView("_CreateNew", new LocalItem());
        }

        [HttpPost]
        public ContentResult CreateNew(LocalItem itemToSave)
        {
            try
            {
                var ret = Manager.SaveLocalItem(itemToSave);
                ContentResult response = null;
                switch (ret)
                {
                    case SaveResults.OK:
                        response = Content("OK");
                        break;
                    case SaveResults.Duplicate:
                        response = Content("The field 'Name' can not be repeated in the same language.");
                        break;
                    case SaveResults.GeneralError:
                    default:
                        response = Content("Genric error while writing data.");
                        break;
                }

                return response;

            }
            catch (System.Exception e)
            {
                return Content("NOK " + e.ToString());
            }
        }

        [HttpGet]
        public PartialViewResult CreateNewEnum()
        {
            return PartialView("_CreateNewEnum");
        }

        [HttpPost]
        public JsonResult CreateNewEnum(string enumName, List<string> enumItems)
        {
            bool ret;
            if (string.IsNullOrEmpty(enumName) || enumItems.Count == 0)
            {
                ret = false;
            }

            foreach (var item in enumItems)
            {
                var itemToSave = new LocalItem();
                itemToSave.ControllerName = LocalizationConstants.ENUM_CONTROLLER_NAME;
                itemToSave.ActionName = enumName;
                itemToSave.Name = item;
                itemToSave.Culture = CultureInfo.CurrentUICulture.Name;
                itemToSave.Value = "To be filled";

                var response = Manager.SaveLocalItem(itemToSave);

                if (response != SaveResults.OK)
                {
                    ret = false;
                    return Json(ret, JsonRequestBehavior.DenyGet);
                }
            }

            ret = true;
            return Json(ret, JsonRequestBehavior.DenyGet);
        }

        [HttpGet]
        [OutputCache(Duration = 0, NoStore = true)]
        public PartialViewResult Edit(string id)
        {
            var tempFilter = new FilterItems { Id = id };
            var itemToEdit = Manager.FilterLocalItems(tempFilter);

            return PartialView("_Edit", itemToEdit.First());
        }

        [HttpPost]
        public ContentResult Edit(LocalItem itemToSave)
        {
            try
            {
                var repo = Manager.SaveLocalItem(itemToSave);
                return Content("OK");
            }
            catch (System.Exception e)
            {
                return Content("NOK");
            }
        }

        [HttpPost]
        public ContentResult DeteleteItem(string id)
        {
            var ret = Manager.DeleteLocalItem(id);
            if (ret)
            {
                return Content("OK");
            }
            else
            {
                return Content("NOK");
            }
        }

        [HttpGet]
        [OutputCache(Duration = 0, NoStore = true)]
        public PartialViewResult Translate(string id)
        {
            var itemToShow = Manager.GetValuesToTranslate(id);
            return PartialView("_Translate", itemToShow);
        }

        [HttpPost]
        public ContentResult SaveTranslation([Bind(Prefix = "item")] SimpleLocal values)
        {
            try
            {
                Manager.SaveTranslation(values.Id, values.Value);
                return Content("OK");
            }
            catch (System.Exception e)
            {

                return Content("NOK");
            }
        }


    }
}
